package cn.com.jrj.base.util;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.ui.velocity.VelocityEngineUtils;

import cn.com.jrj.base.exception.ApplicationException;

public class VelocityUtil {
	private Log log = LogFactory.getLog(getClass());
	private static final VelocityUtil instance = new VelocityUtil();
	private VelocityEngine velocityEngine;

	public static VelocityUtil getInstance() {
		return instance;
	}

	/**
	 * 根据模板生成文件
	 * 
	 * @param vm
	 * @param file
	 * @param context
	 * @return
	 * @throws ShareAppException
	 */
	@SuppressWarnings("unchecked")
	public File mergeToFile(String vm, String file, Map context) throws ApplicationException {
		Writer writer = null;

		context = MapUtils.isEmpty(context) ? new HashMap() : context;
		context.put("StringUtil", new StringUtil());
		context.put("DateUtil", new DateUtil());
		context.put("DecimalUtil", new DecimalUtil());
		context.put("currentDate", new Date());
		try {
			writer = FileUtil.getInstance().getFileWriter(file);
			if (writer == null)
				return null;
			String str = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, vm, context);
			writer.write(str);
			writer.flush();
			return FileUtil.getInstance().getFile(file);
		} catch (ApplicationException e) {
			String msg = getClass().getName() + "#mergeToFile ShareAppException";
			log.error(msg);
			throw e;
		} catch (VelocityException e) {
			String msg = getClass().getName() + "#mergeToFile VelocityException";
			log.error(msg);
			throw new ApplicationException(msg, e);
		} catch (IOException e) {
			String msg = getClass().getName() + "#mergeToFile IOException";
			log.error(msg);
			throw new ApplicationException(msg, e);
		} finally {
			if (writer != null)
				try {
					writer.close();
				} catch (IOException e) {
					String msg = getClass().getName() + "#mergeToFile IOException [close writer error]";
					log.error(msg, e);
				}
		}
	}

	/**
	 * 根据模板生成字符串，返回
	 * 
	 * @param vm
	 * @param context
	 * @return
	 * @throws ShareAppException
	 */
	public String mergeToString(String vm, Map<Object, Object> context) throws ApplicationException {

		try {
			return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, vm, context);
		} catch (VelocityException e) {
			String msg = getClass().getName() + "#mergeToString VelocityException";
			log.error(msg);
			throw new ApplicationException(msg, e);
		}
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}
}
